<template>
  <div class="advert-list">
    <el-card>
      <div slot="header">
        <el-button
          type="primary"
          @click="$router.push({
            name: 'advert-create'
          })"
        >添加广告</el-button>
      </div>
      <el-table
        :data="adverts"
        v-loading="loading"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="spaceId"
          label="广告位置"
          width="120">
          <template slot-scope="scope">
            <div>{{getSpaceName(scope.row)}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="img"
          label="广告图片">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.img"
              fit="cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="时间">
          <template slot-scope="scope">
            <div>{{dateFormatter(scope.row.startTime)}}</div>
            <div>{{dateFormatter(scope.row.endTime)}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="上线/下线">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.isStatusLoading"
              @change="onStateChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="price"
          label="操作"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="$router.push({
                name: 'advert-edit',
                params: {
                  advertId: scope.row.id
                }
              })"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :disabled="loading"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdList, updateStatus } from '@/services/advert'
import { getAllSpaces } from '@/services/advert-space'
import moment from 'moment'

export default Vue.extend({
  name: 'CourseList',
  data () {
    return {
      currentPage: 1,
      pageSize: 10,
      adverts: [],
      totalCount: 0,
      loading: true,
      spaceList: []
    }
  },

  created () {
    this.loadAdverts()
    this.loadSpaces()
  },

  methods: {
    async loadSpaces () {
      const { data } = await getAllSpaces()
      this.spaceList = data.content
    },

    async loadAdverts () {
      this.loading = true
      const { data } = await getAdList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      data.content.forEach((item: any) => {
        item.isStatusLoading = false
      })
      this.adverts = data.content.splice(this.currentPage * this.pageSize, this.pageSize)
      this.totalCount = data.content.length
      this.loading = false
    },

    handleCurrentChange (page: number) {
      this.currentPage = page
      this.loadAdverts()
    },

    dateFormatter (date: Date) {
      return moment(date).format('YYYY-MM-DD')
    },

    getSpaceName (record: any) {
      const spaceRecord = this.spaceList.filter((item: any) => item.id === record.spaceId)
      return spaceRecord.length ? (spaceRecord[0] as any).name : ''
    },

    async onStateChange (adverse: any) {
      adverse.isStatusLoading = true
      await updateStatus(
        adverse.id,
        adverse.status
      )
      this.$message.success(`${adverse.status === 0 ? '下架' : '上架'}成功`)
      adverse.isStatusLoading = false
    }
  }
})
</script>

<style lang="scss" scoped>
.el-card {
  margin-bottom: 20px;
}
</style>
